home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscShell / MiscShellInspector.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  5.1 KB  |  185 lines

  1. // Copyright (C) 1995 Steve Hayman
  2. // Use is governed by the MiscKit license
  3.  
  4. #import "MiscShell.subproj/MiscShell.h"
  5. #import "MiscShellInspector.h"
  6. #import "EmacsText.h"
  7.  
  8. @implementation MiscShellInspector
  9. // Simple view swapping methods in response to the popup
  10. //
  11. - swapToBox:aBox
  12. {
  13.     [swapBox setContentView:[aBox contentView]];
  14.     [swapBox display];
  15.     return self;
  16. }
  17. // the popup buttons send this
  18. - swapView:sender
  19. {
  20.     switch( [[sender selectedCell] tag] ) {
  21.     case 0:    
  22.     [self swapToBox:scriptBox ]; 
  23.     break;
  24.     case 1:    
  25.     [self swapToBox:optionsBox]; 
  26.     break;
  27.     }
  28.     
  29.     return self;
  30. }
  31.  
  32. // Load the Inspector.
  33. // TODO - replace this with an EmacsText object
  34.  
  35. - init
  36. {
  37.     char buf[MAXPATHLEN + 1];
  38.     id bundle;
  39.     NXRect textFrame;
  40.     Font *originalFont;
  41.     
  42.     [super init];
  43.     
  44.     bundle = [NXBundle bundleForClass:[MiscShell class]];
  45.     [bundle getPath: buf forResource: "MiscShellInspector" ofType:"nib"];
  46.     [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
  47.     
  48.     [self swapToBox:scriptBox];
  49.     /*
  50.      * at this point, "text" is pointing to the old scrolling text.
  51.      */
  52.     originalFont = [text font];
  53.  
  54.     /*
  55.      * Now we want to create an EmacsText object and replace the text
  56.      * inside the "document" scroll view with it.
  57.      * Some of this code is borrowed from Scott Anguish's enhanced Yap.
  58.      * Thanks, Scott.
  59.      */
  60.     [[scrollView docView] getFrame:&textFrame];
  61.     text = [[EmacsText alloc] 
  62.     initFrame:&textFrame text:"" alignment:NX_LEFTALIGNED];
  63.     /*
  64.      * Put this new text object in the window and free the IB-created one.
  65.      */
  66.     [[scrollView setDocView:text] free];
  67.     
  68.     /*
  69.      * Finish setting up the new text object
  70.      */
  71.     [text setFont:originalFont];        // Whatever was set in IB
  72.  
  73.     [text setVertResizable:YES];        // Grow down as you type
  74.     [text setHorizResizable:NO];        // But not sideways 
  75.     [text setAutosizing:NX_WIDTHSIZABLE];    // Size horizontally when resized
  76.     [text setMonoFont:YES];
  77.     [text setOpaque:YES];
  78.     [text setMinSize:&textFrame.size];
  79.     NX_WIDTH(&textFrame) = NX_HEIGHT(&textFrame) = 1.0e38;
  80.     [text setMaxSize:&textFrame.size];    // Can grow
  81.     [text setSel:0:0];            // Set the selection
  82.     [text setDelegate:self];
  83.     [text sizeToFit];
  84.            
  85.     return self;
  86. }
  87.  
  88.  
  89. /*
  90.  * revert  - set inspector to reflect object's state
  91.  */
  92. - revert: sender
  93. {
  94.     int tag;
  95.     [text setText: [[object script] stringValue] ];
  96.     [runToCompletionCheck setState:[object runToCompletion]];
  97.     switch( [object delimiter] ) {
  98.     case 0:    tag = 0; break;
  99.     case '\t':    tag = 1; break;
  100.     case ' ':    tag = 2; break;
  101.     case ':':    tag = 3; break;
  102.     default:    tag = 0; break;    // ??? TODO - print an error message
  103.     }
  104.     
  105.     [delimiters selectCellWithTag:tag];
  106.     [customDelimiters setStringValue: [[object customDelimiters] stringValue]];
  107.     [tableSort selectCellWithTag: [object sortWhenColumnsMove]];
  108.     return [super revert:sender];
  109. }
  110.  
  111.  
  112. /*
  113.  * ok - set object to reflect inspector's contents.  various objects in
  114.  * the inspector panel send us this message when they're clicked.
  115.  *
  116.  */
  117. - ok: sender
  118. {
  119.     MiscString *newScript = [[MiscString alloc] init];
  120.     MiscString *aLine = [[MiscString alloc] init];
  121.     NXStream *textStream = [text stream];
  122.     char newDelim;
  123.     MiscString *newDelimiters = [[MiscString alloc] init];
  124.     
  125.     [self touch:self];            // mark inspector panel edited
  126.     [[NXApp activeDocument] touch];    // mark nib as edited
  127.     /*
  128.      * The textStream isn't necessarily at the start when we do [text stream];
  129.      * it might be where we left it before.  which could cause us to
  130.      * hit EOS immediately.  which did happen.  which drove me nuts for
  131.      * a while as my carefully setup scripts kept vanishing in the inspector.
  132.      */
  133.     NXSeek(textStream, 0L, NX_FROMSTART);
  134.     
  135.     while ( [aLine streamGets:textStream] != EOF) {
  136.     [newScript concatenate:aLine];
  137.     }
  138.     [newScript concatenate:aLine];    // last incomplete line if any
  139.  
  140.     [object setScript:newScript];    // it makes a copy
  141.    
  142.     [newDelimiters setStringValue: [customDelimiters stringValue]];
  143.     [object setCustomDelimiters:newDelimiters];    // it makes a copy
  144.     [newDelimiters free];
  145.     
  146.     [newScript free];
  147.     [aLine free];
  148.     
  149.     [object setRunToCompletion: [runToCompletionCheck state]];
  150.     
  151.     switch ( [[delimiters selectedCell] tag] ) {
  152.     case 0:    newDelim = 0; break;
  153.     case 1:    newDelim = '\t'; break;
  154.     case 2:    newDelim = ' '; break;
  155.     case 3:    newDelim = ':'; break;
  156.     default:    newDelim = 0; break;    // ??? todo - add an error msg
  157.     }
  158.     
  159.     [object setDelimiter:newDelim];
  160.     [object setSortWhenColumnsMove: [[tableSort selectedCell] tag]];
  161.     return self;
  162. }
  163. - (BOOL)wantsButtons
  164. {
  165.     return NO;
  166. }
  167.  
  168. // Text delegate methods - we're the delegate of the script text, so we
  169. // want to do "ok" whenever the text changes.
  170. // thanks to the StringList palette for this clever method.
  171. - textDidGetKeys:sender isEmpty:(BOOL)flag 
  172. {
  173.     [self perform:@selector(ok:) with:self afterDelay:500 cancelPrevious:YES];
  174.     return self; 
  175. }
  176.  
  177. // same deal for text fields
  178. - textDidChange:sender
  179. {
  180.     [self perform:@selector(ok:) with:self afterDelay:500 cancelPrevious:YES];
  181.     return self;
  182. }
  183.  
  184. @end
  185.